package com.bsu.bsurokon.scene;

import android.util.Log;
import android.view.MotionEvent;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.MassData;
import com.badlogic.gdx.physics.box2d.World;
import com.bsu.bsurokon.BSURokon;
import com.bsu.bsurokon.GameTextures;
import com.stickycoding.rokon.MathHelper;
import com.stickycoding.rokon.PhysicalSprite;
import com.stickycoding.rokon.Scene;
import com.stickycoding.rokon.Sprite;
import com.stickycoding.rokon.Window;
import com.stickycoding.rokon.background.FixedBackground;

public class PhysicalScene extends Scene{
	private final static int BOX_COUNT = 10;
	private World world;
	private FixedBackground background;
	private Vector2 gravity;
	private PhysicalSprite[] wall, box;
	private Sprite[] sprite;
	
	public PhysicalScene(){
		super(4,new int[]{1,BOX_COUNT,10,10});
		setBackground(background = new FixedBackground(GameTextures.gameBack));
		
		setWorld(world = new World(gravity = new Vector2(0f,10f),false));
		createWalls();
		createBoxes();
		//createSprites();
	}
	
	private void createWalls(){
		wall = new PhysicalSprite[4];
		wall[0] = new PhysicalSprite(0,-1,BSURokon.GAME_WIDTH,1);
		wall[0].createStaticBox();
		add(0,wall[0]);
		
		wall[1] = new PhysicalSprite(BSURokon.GAME_WIDTH,0,1,BSURokon.GAME_HEIGHT);
		wall[1].createStaticBox();
		add(0,wall[1]);
		
		wall[2] = new PhysicalSprite(0,BSURokon.GAME_HEIGHT,BSURokon.GAME_WIDTH,1);
		wall[2].createStaticBox();
		add(0,wall[2]);
		
		wall[3] = new PhysicalSprite(-1,0,1,BSURokon.GAME_HEIGHT);
		wall[3].createStaticBox();
		add(0,wall[3]);
	}
	
	private void createBoxes(){
		FixtureDef fixtureDef = new FixtureDef();
		fixtureDef.density = 1;										//设置密度
		fixtureDef.friction = 0f;									//设置摩擦系数
		fixtureDef.restitution = 1f;								//设置弹性物质复原系数
		
		box = new PhysicalSprite[BOX_COUNT];
		for(int i=0;i<BOX_COUNT;i++){
			box[i] = new PhysicalSprite((float)Math.random()
				* BSURokon.GAME_WIDTH,(float)Math.random()
				* BSURokon.GAME_HEIGHT,(float)GameTextures.wood_rectangle.getWidth(),
				(float)GameTextures.wood_rectangle.getHeight());
			box[i].setTexture(GameTextures.wood_rectangle);
			box[i].setRGBA(1, 1, 1, 1);
			box[i].createDynamicBox(fixtureDef);
//			box[i].body.setUserData(box[i]);
			//box[i].createKinematicBox();
			box[i].setBorder(1, 1, 1, 1);
			//box[i].
			add(1,box[i]);
		}
	}
	
	private void createSprites(){
		sprite = new Sprite[3];
		sprite[0] = new Sprite(100, 220, GameTextures.wood_rectangle.getWidth(), GameTextures.wood_rectangle.getHeight());
		sprite[0].setTexture(GameTextures.wood_rectangle);
		sprite[1] = new Sprite(150, 220, GameTextures.wood_rectangle.getWidth(), GameTextures.wood_rectangle.getHeight());
		sprite[1].setTexture(GameTextures.wood_rectangle);
		sprite[2] = new Sprite(200, 220, GameTextures.wood_rectangle.getWidth(), GameTextures.wood_rectangle.getHeight());
		sprite[2].setTexture(GameTextures.wood_rectangle);
		
		
		add(2,sprite[0]);
		add(2,sprite[1]);
		add(2,sprite[2]);
	}
	
	int x=0,y=0;
	@Override
	public void onTouchDown(float x, float y, MotionEvent event,
			int pointerCount, int pointerId){
		for(int i =0;i<box.length;i++){
			if(MathHelper.pointInShape(x, y, box[i])){
				//box[i].accelerateY(-5f);
				//box[i].setSpeed(0, -5);
				//box[i].setTerminalSpeedY(-1.5f);
				//box[i].moveTo(100, 100, 1000);
				
				MassData md = new MassData();
				md.mass = 0.1f;
				box[i].body.setMassData(md);
				box[i].body.setAwake(true);
				
				Vector2 force = new Vector2(2000,-2000);
				
				//force.dst(x, y);
				//force.mul(1f);
				box[i].body.applyForce(force, box[i].body.getPosition());
				box[i].body.applyAngularImpulse(3.0f);
				box[i].body.applyTorque(0.5f);
			}
		}

		
		//gravity.set(0,0.5f);
		//gravity.set(0.1f,0.1f);
		//world.setGravity(gravity);
		//Log.i("","xx=" + world.getGravity().x + " yy=" + world.getGravity().y);
	}
	
	int loop = 0;
	boolean leftflag = true;
	@Override
	public void onGameLoop() {
		

	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onReady() {
		// TODO Auto-generated method stub
//		for(int i=0;i<sprite.length;i++)
//		{
//			sprite[i].moveTo(100, 100, 5000);
//		}
		//sprite[0].moveTo(100, 100, 5000);
		//box[0].moveTo(100,100,5000);
		//box[0].accelerateY(0.5f);
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		
	}

}
